/*******************************************************************************
* This file implements the data logging functions to the serial flash.
*
* Author: Kong Wai Weng @ Cytron Technologies Sdn. Bhd.
*******************************************************************************/



#ifndef _DATALOG_H
#define _DATALOG_H



#include <stdio.h>
#include <string.h>
#include "FreeRTOS.h"
#include "task.h"
#include "variables.h"
#include "hal_uart.h"
#include "hal_serialflash.h"
#include "hal_gps.h"
#include "hal_ic.h"
#include "dcm.h"
#include "airspeed.h"
#include "hal_bmp085.h"
#include "navigation.h"



/*******************************************************************************
* STRUCTURE AND UNION DEFINITION                                               *
*******************************************************************************/

// Log Index.
struct LOG_INDEX {
	// Log ID (0 - 254).
	unsigned char ucLogId;
	
	// MSB of the Start Address Offset (Must be aligned to 4KB boundary.
	unsigned char ucAddOffsetMsb;
};	



// Log Descriptor.
// Every log entry must begin with this data.
struct LOG_DESCRIPTOR {
	// Log ID (0 - 254).
	unsigned char ucLogId;
	
	// The date and time when the log is created.
	struct RTC_DATA xDateTime;
};	



// Log Data.
// These are the data that will be logged to the serial flash.
struct LOG_DATA {
	// Control mode.
	// 0 = Manual, 1 = Auto.
	unsigned char ucMode;
	
	// GPS fix status.
	// 0 = Not fixed, 1 = Fixed.
    unsigned char ucFixStatus;
    
	// Coordinate.
	// Unit = degree.
	float fLatitude;
	float fLongitude;
	
	// GPS speed.
	// Unit = km/h.
	unsigned int uiGpsSpeed;
	
	// GPS heading (0 - 359).
	// Unit = degree.
	unsigned int uiGpsHeading;
	
	// GPS altitude.
	// Unit = m.
	signed int siGpsAltitude;
	
	
	
	// Airspeed.
	// Unit = km/h.
	unsigned int uiAirspeed;
	
	// Magnetic heading (0 - 359).
	// Unit = degree.
	unsigned int uiMagHeading;
	
	// Pressure altitude.
	// Unit = m.
	signed int siPressureAltitude;
	
	
	// Orientation.
	// Unit = degree * 10.
	signed int siRoll;
	signed int siPitch;
	signed int siYaw;
	
	// Heading Setpoint.
	// Unit = degree * 10.
	unsigned int uiHeadingSetpoint;
	
	// Servo output.
	unsigned int uiServo1;
	unsigned int uiServo2;
	unsigned int uiServo3;
	unsigned int uiServo4;
	unsigned int uiServo5;
	unsigned int uiServo6;
	
	
	// Time.
    unsigned char ucHour;
    unsigned char ucMinute;
    unsigned char ucSecond;
	
	// Log ID (0 - 254).
	// This byte also indicates that this is a valid data.
	// We put it as a last bytes to make sure the data is successfully written to the flash.
	unsigned char ucLogId;
};	



/*******************************************************************************
* PUBLIC FUNCTION PROTOTYPES                                                   *
*******************************************************************************/

/*******************************************************************************
* TASK: taskDataLog
*
* DESCRIPTIONS:
* This task logs the flight data to the serial flash.
*
*******************************************************************************/
portTASK_FUNCTION_PROTO(taskDataLog, pvParameters);



/*******************************************************************************
* PUBLIC FUNCTION: vMemoryDump
*
* PARAMETERS:
* ~ ulStartAdd	- Start address.
* ~ ulEndAdd	- End address.
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Send the content of the memory to PC.
* This is a blocking function and should not be called from ISR.
*
*******************************************************************************/
void vMemoryDump(unsigned long ulStartAdd, unsigned long ulEndAdd);



/*******************************************************************************
* PUBLIC FUNCTION: vPrintLogData
*
* PARAMETERS:
* ~ ucLogId	- Log ID of the data we want to print.
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Send the content of the log data to PC.
* The data will be sent as CSV format.
* This is a blocking function and should not be called from ISR.
*
*******************************************************************************/
void vPrintLogData(unsigned char ucLogId);



/*******************************************************************************
* PUBLIC FUNCTION: vListLog
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* List out all the log entry together with it's data and time.
* This is a blocking function and should not be called from ISR.
*
*******************************************************************************/
void vListLog(void);



#endif
